package com.kj.xuyuan.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.LoginExplainAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.LoginExplainBean;
import com.kj.xuyuan.model.UserTagBean;
import com.kj.xuyuan.util.KeyboardUtil;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.kj.xuyuan.widget.WebViewActivity;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginExplainActivity extends BaseActivity {

    @BindView(R.id.ed_search)
    EditText edSearch;
    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.rv_question)
    RecyclerView rvQuestion;
    @BindView(R.id.sml_question)
    SmartRefreshLayout smlQuestion;

    private int page = 1;
    private List<LoginExplainBean> data;
    private LoginExplainAdapter adapter;

    public static void actionStar(Context context){
        Intent intent = new Intent(context,LoginExplainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_login_explain;
    }

    @Override
    protected void initView() {
        setTitleText("登录说明");

        //rvQuestion  item  item_question

        edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if(!StringUtils.isEmpty(edSearch.getText().toString().trim())){
                    if(actionId == EditorInfo.IME_ACTION_SEARCH){
                        //搜索
                        page = 1;
                        getQuestion(edSearch.getText().toString().trim());
                    }
                }
                return false;
            }
        });

        initRecyclerView();
        getQuestion("");
    }

    private void initRecyclerView() {
        data = new ArrayList<>();
        adapter = new LoginExplainAdapter();
        rvQuestion.setLayoutManager(new LinearLayoutManager(context));
        rvQuestion.setAdapter(adapter);
        adapter.setNewData(data);

        smlQuestion.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getQuestion("");
            }
        });

        smlQuestion.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getQuestion("");
            }
        });

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WebViewActivity.actionStar(context,data.get(position).getId());
            }
        });
    }

    private void getQuestion(String search) {
        Map map = new HashMap();
        map.put("page",page+"");
        map.put("search",search);

        OkGoUtil.postRequest(getContext(), UrlConstants.LOGIN_EXPLAIN, map, new JsonCallback<BaseModel<List<LoginExplainBean>>>() {
            @Override
            public void onSuccess(Response<BaseModel<List<LoginExplainBean>>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        if(page == 1){
                            data.clear();
                        }
                        data.addAll(response.body().getData());
                        adapter.notifyDataSetChanged();
                        smlQuestion.finishRefresh();
                        if(page > 1 && (response.body().getData() == null || response.body().getData().size() == 0)){
                            smlQuestion.finishLoadMoreWithNoMoreData();
                        }else{
                            smlQuestion.finishLoadMore();
                        }
                    }else{
                        ToastUtil.showToast(context,response.body().getMsg());
                        smlQuestion.finishRefresh();
                        smlQuestion.finishLoadMore();
                    }
                }
            }

            @Override
            public void onError(Response<BaseModel<List<LoginExplainBean>>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                smlQuestion.finishRefresh();
                smlQuestion.finishLoadMore();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}